<template>
  <div>
    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel
          @formChange="changeHandle"
          ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :isSubmitBtn="false"/>
      </div>
    </el-col>

    <div class="dialogBottmView">

      <multiuse-button type="primary" class="btnStyle" v-if="handleType=='add'" :click="closeMenu.bind(this,1)">保存
      </multiuse-button>

      <multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this,2)">关闭</multiuse-button>

    </div>

    <BaseDialog :visible.sync="visiblePeij" title="配件选择"
                :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
                destroyOnClose>
      <add-peij
        @selectArr="selectPeijFun"
        @close='closeDialog'></add-peij>
    </BaseDialog>

  </div>
</template>

<script>
  import {addThreeAssemblyPart} from '@/api/repair/threePacksOfManager/assemblyAndAccessoriesJingxsDuan';
  import moment from 'moment';
  import {dictionary} from '@/utils/dictMixin';
  import {isEmpty} from '@/utils/utils';
  import {exportExcelFile} from '@/utils/index';
  import addPeij from './addPeij'

  export default {
    name: 'add',
    mixins: [dictionary],
    props: ['handleType', 'formData'],
    data()
    {
      this.saveData = null;
      return {
        formList: this.createFormList(),
        detailObj: {},
        username: '',
        visiblePeij: false
      };
    },
    components: {addPeij},
    mounted()
    {
      this.getData();
    },
    methods: {

      changeHandle(val)
      {
        this.saveData = val;
      },

      closeDialog()
      {
        this.visiblePeij = false
      },


      /*配件 */
      selectPeijFun(arr)
      {

        this.closeDialog()

        this.formList.find(item => item.fieldName == 'partNo').initialValue = arr[0].partCode
        this.formList.find(item => item.fieldName == 'partName').initialValue = arr[0].partName

      },

      /*根据ID查询*/
      async getData()
      {
        let res = this;

        let data = isEmpty(this.formData) ? {} : this.formData;

        for (let i in data)
        {
          this.formList.map(x =>
          {
            if (i == x.fieldName)
            {
              x.disabled = true
              x.initialValue = data[i];
            }
          });
        }

      },
      async closeMenu(type)
      {
        if (type == 1)
        {
          /*保存数据*/
          this.$refs.formPanel.SUBMIT_FORM();

          if (this.saveData)
          {
            let obj = this.saveData

            // if (this.handleType == 'add')
            // {
            //   obj.createdBy = Cookies.get('userId', "")
            // }

            let res = await addThreeAssemblyPart(obj)
            if (res.resultCode == 200 && res.success == true)
            {
              this.$emit('close', 'refresh')
            } else
            {
              let errMsg = isEmpty(res.errMsg) ? '操作失败' : res.errMsg;

              this.$notify.error({title: '提示信息', message: errMsg});
            }
          }

        } else
        {

          this.$emit('close')
        }
      },
      /*查询配件代码列表*/
      seacherFun()
      {
        this.visiblePeij = true
      },

      createFormList()
      {

        return [
          {
            type: 'BREAK_SPACE',
            label: '三包配件与总成关系',
          },

          {
            type: 'INPUT',
            label: '配件代码',
            fieldName: 'partNo',
            placeholder: '',
            disabled: false,
            readonly: true,
            rules: [{required: true, message: '总成代码不能为空', trigger: 'change'}],
            initialValue: '',
            itemList: this.createDictList(1023),
            unitRender: () =>
            {
              return (
                <div>
                  <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
                </div>
              );
            },

          },
          {
            type: 'INPUT',
            label: '配件名称',
            fieldName: 'partName',
            placeholder: '请选择',
            disabled: false,
            initialValue: '',

          },
          {
            type: 'SELECT',
            label: '总成代码',
            fieldName: 'assemblyCode',
            placeholder: '',
            disabled: false,
            rules: [{required: true, message: '总成代码不能为空', trigger: 'change'}],
            initialValue: '',
            itemList: this.createDictList(1023)

          },
          {
            type: 'INPUT',
            label: '总成名称',
            fieldName: 'assemblyName',
            placeholder: '请选择',
            disabled: false,
          },
          {
            type: 'TEXT_AREA',
            label: '关系备注',
            fieldName: 'remarks',
            placeholder: '请选择',
            disabled: false,
            rules: [{required: false, trigger: 'change'}],
            initialValue: '',
            selfCols: 3
          },
        ];
      },
    }
  };
</script>

<style scoped lang="less">
  .bg1 {
    border: 1px solid @borderColor;
    border-radius: @borderRadius;
    box-shadow: @boxShadow;
    padding: @modulePadding;
    padding-left: 0;
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .bg2 {
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .btn {
    text-align: center;
  }

  .btnStyle {
    margin-top: 10px;
  }
</style>
